/*
 * @Author: LVGRAPE
 * @Date: 2023-08-31 10:36:20
 * @LastEditTime: 2025-07-07 16:00:34
 * @LastEditors: LVGRAPE
 * @Description:
 * @FilePath: \zino-fc-v4\ZINO\hardware\pwr\pwr_key.c
 * 可以输入预定的版权声明、个性签名、空行等
 */
#include "zino.h"
#include "drv_pin.h"
#include "rc.h"
#include "drv_common.h"
#include <stdbool.h>
#include "fc_state.h"
#include "fc_sensors.h"
#include "drv_adc.h"

#define DBG_TAG "pwr_key"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>


#define PWR_KEY_PIN PA0

#define KEY_DOWN_MASK 0X80       /**按下标记*/
#define KEY_LONG_PRESS_MASK 0X40 /**长按标记 */
#define KEY_TIMEOUT_MASK 0X10    /**超时标志，此时返回按键值*/
#define KEY_TIEMES_MASK 0X0F     /**按了多少次 */
#define KEY_VALUE(x) (0x0001 << (x))

#define KEY_COUNTS 1

const uint8_t SHORT_PRESS_TIME = 25;
const uint8_t LONG_PRESS_TIME = 100;
const uint8_t IS_KEY_DOWN = 0X80;   /**按下了 */
const uint8_t IS_LONG_PRESS = 0X40; /**长按了 */
const uint8_t IS_TIME_OUT = 0X10;   /**退好久没按 */
static uint8_t pressTimesRecord[KEY_COUNTS] = {0};
static uint8_t pressTime[KEY_COUNTS] = {0};
static uint8_t longpressTime[KEY_COUNTS] = {0};
const uint16_t channel_keyscan_map[KEY_COUNTS] = {1}; /**A,B,C,D,E,F,G,H,I,J,K,L对就的键值*/
void keyScanPro_10ms(void)
{
    pin_mode(PWR_KEY_PIN, PIN_MODE_INPUT_PULLUP);
    uint16_t keyvalu = pin_read(PWR_KEY_PIN);
    for (uint8_t i = 0; i < KEY_COUNTS; i++)
    {
        if (keyvalu == channel_keyscan_map[i])
        {
            // 按下了
            if (longpressTime[i] < LONG_PRESS_TIME)
            {
                longpressTime[i]++;
                pressTime[i] = SHORT_PRESS_TIME;
                pressTimesRecord[i] |= IS_KEY_DOWN;
            }
            else
            {
                pressTimesRecord[i] |= IS_LONG_PRESS;
                pressTimesRecord[i] |= IS_TIME_OUT;
                pressTimesRecord[i] &= ~IS_KEY_DOWN; // 取消标记高位
                LOG_I("long press\n");
            }
            // LOG_D("key %d down\n");
        }
        else
        {
            // 松开了
            longpressTime[i] = 0;
            if (pressTimesRecord[i] & IS_KEY_DOWN)
            {
                // 高位标记过，即按下过
                pressTimesRecord[i] &= ~IS_KEY_DOWN; // 取消标记高位
                if ((pressTimesRecord[i] & KEY_TIEMES_MASK) < 15)
                {
                    pressTimesRecord[i]++;
                    // LOG_D("keyr %d up\n",pressTimesRecord[i]);
                }
                // LOG_D("key %d up %d\n",i,pressTimesRecord[i]);
            }
            if (pressTime[i] > 0)
            {
                if (pressTime[i] == 1)
                {
                    pressTimesRecord[i] |= IS_TIME_OUT; // BIT4 为检测时间到
                    // LOG_D("key %d timeout r:%d\n",i,pressTimesRecord[i]);
                }
                pressTime[i]--;
            }
            if (pressTimesRecord[i] & IS_TIME_OUT)
            {

                if (pressTimesRecord[i] & IS_LONG_PRESS)
                {
                    rt_kprintf("Long press:%d \n", i);
                    pressTimesRecord[i] &= ~IS_LONG_PRESS;
                }
                else if (pressTimesRecord[i] & KEY_TIEMES_MASK)
                {
                    uint8_t ptc = pressTimesRecord[i] & KEY_TIEMES_MASK;
                    LOG_I("Press:%d - %d\n", i, ptc);
                    if (ptc == 1)
                    {
                        show_battery_led();
                        LOG_D("battery:%d mv", get_vbat_voltage_mv());
                    }
                    else if (ptc == 2)
                    {
                        rc_key_action_switch();
                    }else if (ptc == 3)
                    {
                        AccStartCalibration();
                        GyroStartCalibration();
                        LOG_D("IMU CALIBRATIING...");
                    }
                }
                pressTimesRecord[i] = 0;
            }
        }
    }
}

uint8_t get_pwr_key_action(void)
{
    uint8_t ptc = pressTimesRecord[0] & KEY_TIEMES_MASK;
    // if (reset && ptc)
    // {
    //     pressTimesRecord[0] = 0;
    // }
    return ptc;
}
void clear_pwr_key_action(void)
{
    pressTimesRecord[0] = 0;
}
